var pager;

function Pager(tableName, itemsPerPage, pagerName, positionId) {
    this.tableName = tableName;
    this.itemsPerPage = itemsPerPage;
	this.pagerName = pagerName;
	this.positionId = positionId;
    this.currentPage = 1;
    this.pages = 0;
    this.inited = false;
    
    this.showRecords = function(from, to) {        
        var rows = document.getElementById(tableName).rows;
        // i starts from 1 to skip table header row
        for (var i = 0; i < rows.length; i++) {
            if (i < from || i > to)  
                rows[i].style.display = 'none';
            else
            {
				if ((i - from) % 2 == 0)
					rows[i].style.background = '#C5FEFE';
				else
					rows[i].style.background = 'white';
				rows[i].style.display = '';
			}
        }
    }
    
    this.showPage = function(pageNumber) {
    	if (! this.inited) {
    		alert("not inited");
    		return;
    	}
        
        this.currentPage = pageNumber;
		this.showPageNav();
        
        var from = (pageNumber - 1) * itemsPerPage;
        var to = from + itemsPerPage - 1;
        this.showRecords(from, to);
    }   
    
    this.prev = function() {
        if (this.currentPage > 1)
            this.showPage(this.currentPage - 1);
    }
    
    this.next = function() {
        if (this.currentPage < this.pages) {
            this.showPage(this.currentPage + 1);
        }
    }                        
    
    this.init = function() {
        var rows = document.getElementById(tableName).rows;
        var records = rows.length; 
        this.pages = Math.ceil(records / itemsPerPage);
		$("#"+this.positionId).show();
        this.inited = true;
    }

    this.showPageNav = function() {
    	if (! this.inited) {
    		alert("not inited");
    		return;
    	}
		var curPage = this.currentPage, pageCount = this.pages;
    	var element = document.getElementById(positionId);
		var pagerHtml = "";
    	//Left arrow
		if (curPage > 1)
    		pagerHtml += '<span onclick="' + pagerName + '.prev();" class="pg-pointer">&larr;</span>&nbsp;&nbsp;';
		//First page
		if (this.currentPage > 2)
			pagerHtml += '<span id="pg' + 1 + '" class='+category(1,curPage)+' onclick="' + pagerName + '.showPage(' + 1 + ');">' + 1 + '</span>&nbsp;&nbsp;';
		//Three dots
		if (this.currentPage > 3)
			pagerHtml += "...&nbsp;&nbsp;";
		//Three middle pages
        for (var page = Math.max(1,curPage-1); page <= Math.min(pageCount,curPage+1); page++) 
            pagerHtml += '<span id="pg' + page + '" class='+category(page,curPage)+' onclick="' + pagerName + '.showPage(' + page + ');">' + page + '</span>&nbsp;&nbsp;';
		//Three dots
		if (this.currentPage < pageCount - 2)
			pagerHtml += "...&nbsp;&nbsp;";
		//Last page
		if (this.currentPage < pageCount - 1)
			pagerHtml += '<span id="pg' + pageCount + '" class='+category(pageCount,curPage)+' onclick="' + pagerName + '.showPage(' + pageCount + ');">' + pageCount + '</span>&nbsp;&nbsp;';
		//Right arrow
		if (curPage < pageCount)
        	pagerHtml += '<span onclick="'+pagerName+'.next();" class="pg-pointer">&rarr;</span>';            
        
        element.innerHTML = pagerHtml;
    }
	
	function category(number,curPage) {
		return (number == curPage ? "pg-selected" : "pg-normal");
	}
}

